package com.quartet.modules.terminal.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.quartet.api.ApiResult;
import com.quartet.api.PageResult;
import com.quartet.modules.customer.entity.TCustomerInfo;
import com.quartet.modules.customer.service.TCustomerInfoService;
import com.quartet.modules.order.entity.TOrder;
import com.quartet.modules.order.service.TOrderService;
import com.quartet.modules.terminal.dto.TFlowCardInfoAddRequestDto;
import com.quartet.modules.terminal.dto.TFlowCardInfoQueryRequestDto;
import com.quartet.modules.terminal.dto.TFlowCardInfoResponseDto;
import com.quartet.modules.terminal.entity.TFlowCardInfo;
import com.quartet.modules.terminal.entity.TTerminalInfo;
import com.quartet.modules.terminal.service.TFlowCardInfoService;
import com.quartet.modules.terminal.service.TTerminalInfoService;
import com.quartet.utils.BeanTool;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 流量卡信息表(TFlowCardInfo)表控制层
 *
 * @author makejava
 * @since 2021-06-14 09:20:40
 */
@RestController
@RequestMapping("tFlowCardInfo")
@Slf4j
@Api(tags = "业务：流量卡管理")
public class TFlowCardInfoController  {
    /**
     * 服务对象
     */
    @Resource
    private TFlowCardInfoService tFlowCardInfoService;

    /**
     * 服务对象
     */
    @Resource
    private TTerminalInfoService tTerminalInfoService;

    /**
     * 服务对象
     */
    @Resource
    private TCustomerInfoService tCustomerInfoService;
    /**
     * 服务对象
     */
    @Resource
    private TOrderService tOrderService;

    /**
     * 分页查询所有数据
     *
     * @param tFlowCardInfoQueryRequestDto 查询实体
     * @return 所有数据
     */
    @PostMapping("/selectFlowCardPage")
    public ApiResult<PageResult<TFlowCardInfoResponseDto>> selectFlowCardPage(@RequestBody TFlowCardInfoQueryRequestDto tFlowCardInfoQueryRequestDto) {
        log.info("---------selectFlowCardPage begin, tFlowCardInfoQueryRequestDto is {}----------", tFlowCardInfoQueryRequestDto);
        PageResult<TFlowCardInfoResponseDto> page = tFlowCardInfoService.selectFlowCardPage(tFlowCardInfoQueryRequestDto);
        log.info("---------selectFlowCardPage end, data is {}----------", page);
        return ApiResult.ok(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getFlowCardById")
    public ApiResult<TFlowCardInfoResponseDto> getFlowCardById(@RequestParam("id") Integer id) {
        TFlowCardInfo tFlowCardInfo = tFlowCardInfoService.getById(id);
        TFlowCardInfoResponseDto tFlowCardInfoResponseDto = BeanUtil.copyProperties(tFlowCardInfo, TFlowCardInfoResponseDto.class);

        QueryWrapper<TTerminalInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TTerminalInfo::getFlowCardNumber, tFlowCardInfoResponseDto.getFlowCardNumber());
        TTerminalInfo tTerminalInfo = tTerminalInfoService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(tTerminalInfo)) {
            return ApiResult.ok(tFlowCardInfoResponseDto);
        }

        if (StringUtils.isBlank(tTerminalInfo.getOrderCode())){
            return ApiResult.ok(tFlowCardInfoResponseDto);
        }
        TOrder tOrder = tOrderService.getOne(new QueryWrapper<TOrder>().lambda().eq(TOrder::getOrderCode, tTerminalInfo.getOrderCode()));
        if (ObjectUtils.isEmpty(tOrder)){
            return ApiResult.ok(tFlowCardInfoResponseDto);
        }

        QueryWrapper<TCustomerInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(TCustomerInfo::getCustomerCode, tOrder.getCustomerCode());
        TCustomerInfo tCustomerInfo = tCustomerInfoService.getOne(queryWrapper1);
        if (ObjectUtils.isEmpty(tCustomerInfo)) {
            return ApiResult.ok(tFlowCardInfoResponseDto);
        }
        tFlowCardInfoResponseDto.setCustomerCode(tOrder.getCustomerCode());
        tFlowCardInfoResponseDto.setCustomerName(tCustomerInfo.getCustomerName());
        return ApiResult.ok(tFlowCardInfoResponseDto);
    }

    /**
     * 新增数据
     *
     * @param tFlowCardInfoAddRequestDto 实体对象
     * @return 新增结果
     */
    @PostMapping("/addFlowCard")
    public ApiResult addFlowCard(@RequestBody TFlowCardInfoAddRequestDto tFlowCardInfoAddRequestDto) {
        TFlowCardInfo tFlowCardInfo = BeanUtil.copyProperties(tFlowCardInfoAddRequestDto, TFlowCardInfo.class);
        return ApiResult.ok(this.tFlowCardInfoService.save(tFlowCardInfo));
    }

    /**
     * 修改数据
     *
     * @param tFlowCardInfoAddRequestDto 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateFlowCard")
    public ApiResult updateFlowCard(@RequestBody TFlowCardInfoAddRequestDto tFlowCardInfoAddRequestDto) {
        TFlowCardInfo tFlowCardInfo = BeanUtil.copyProperties(tFlowCardInfoAddRequestDto, TFlowCardInfo.class);
        return ApiResult.ok(this.tFlowCardInfoService.updateById(tFlowCardInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("deleteFlowCard")
    public ApiResult deleteFlowCard(@RequestParam("id") Integer id) {
        return ApiResult.ok(this.tFlowCardInfoService.removeById(id));
    }

    /**
     * 流量卡下拉列表
     *
     * @return
     */
    @GetMapping("/selectFlowCardList")
    public ApiResult<List<TFlowCardInfoResponseDto>> selectFlowCardList() {
        List<TFlowCardInfo> tFlowCardInfoList = tFlowCardInfoService.list();
        List<TFlowCardInfoResponseDto> tFlowCardInfoResponseDtoList = BeanTool.copyObjListProperties(tFlowCardInfoList, TFlowCardInfoResponseDto.class);
        return ApiResult.ok(tFlowCardInfoResponseDtoList);
    }
}
